package com.java.basis.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author wangning
 * @create 2021-04-25 9:15
 */
public class  TestCallableThread {

	public static void main(String[] args) throws ExecutionException, InterruptedException {
		//1.创建Callable
		MyCallable myCallable = new MyCallable();
		//2.创建FutureTask，传入callable
		FutureTask futureTask = new FutureTask<>(myCallable);
		//3.创建线程
		Thread t1 = new Thread(futureTask);
		t1.start();
		Object count = futureTask.get();
		System.out.println("和为：" + count);
	}
}
class MyCallable implements Callable {

	@Override
	public Object call() throws Exception {
		int count = 0;
		for (int i = 0; i < 10; i++) {
			count += i;
		}
		return count;
	}
}
